package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/4/15 09:57
 * @apiNote
 */

public class Test00405ToHex {

    public String toHex(int num) {
        if (num == 0){
            return "0";
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 7;i >= 0;i--){
            int val = (num >> (4 * i)) & 0xf;
            if(val > 0 || sb.length() > 0){
                char digit = val < 10 ? (char) ('0' + val) : (char) ('a' + val - 10);
                sb.append(digit);
            }
        }
        return sb.toString();
    }
}
